package com.yegs.admin.business.controler;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yegs.admin.business.entity.BaseMenu;
import com.yegs.admin.business.entity.BaseRole;
import com.yegs.admin.business.serviceImpl.BaseMenuServiceImpl;
import com.yegs.admin.config.Msg;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yegs
 * @since 2020-08-28
 */
@RestController
@RequestMapping("/baseMenu")
public class BaseMenuController {

    @Autowired
    private BaseMenuServiceImpl baseMenuService;



    @GetMapping("/delbyid")
    @RequiresPermissions("baseMenu:delbyid")
    public Msg delById(@RequestParam("id") Long id){
        try {
            baseMenuService.removeById(id);
            return Msg.success("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return Msg.error(e.getMessage());
        }
    }

    //保存||更新
    @PostMapping("/save")
    @RequiresPermissions("baseMenu:save")
    public Msg save(@RequestBody BaseMenu t){
        try {
            baseMenuService.saveOrUpdate(t);
            return Msg.success("保存成功!");
        }catch (Exception e){
            e.printStackTrace();
            return Msg.error(e.getMessage());
        }
    }

    //查询
    @PostMapping("/query")
    @RequiresPermissions("baseMenu:query")
    public Page<BaseRole> query(BaseMenu t){
        Page page = new Page(0,5);
        QueryWrapper<BaseMenu> queryWrapper = new QueryWrapper<BaseMenu>();
        queryWrapper.setEntity(t);
        return baseMenuService.page(page,queryWrapper);
    }
    //查询
    @PostMapping("/getMenus")
    @RequiresPermissions("baseMenu:getMenus")
    public Msg getMenus(BaseMenu t){
        Page page = new Page(0,10);
        QueryWrapper<BaseMenu> queryWrapper = new QueryWrapper<BaseMenu>();
        queryWrapper.setEntity(t);
        page = baseMenuService.page(page,queryWrapper);
        HashMap hashMap = new HashMap();
        hashMap.put("list",page.getRecords());
        hashMap.put("total",page.getTotal());
        return Msg.success(hashMap);
    }
    @GetMapping("/getTree")
    @RequiresPermissions("baseMenu:getTree")
    public Msg getTree(){
        List<BaseMenu> tree = baseMenuService.getTree();
        HashMap hashMap = new HashMap();
        hashMap.put("tree",tree);
        return Msg.success(hashMap);
    }
}
